package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.annotation.ApiAuth;
import com.rustic.server.dto.TravelPlanAddDTO;
import com.rustic.server.dto.TravelPlanUpdateDTO;
import com.rustic.server.enums.LinkType;
import com.rustic.server.po.TravelPlan;
import com.rustic.server.query.TravelPlanUserListQuery;
import com.rustic.server.service.CommentService;
import com.rustic.server.service.TravelPlanService;
import com.rustic.server.vo.TravelPlanWebVO;
import com.rustic.server.vo.list.TravelPlanWebListVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[travel_plan]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/travelPlan")
@Api(tags = {"旅游攻略接口"})
public class TravelPlanController {

    @Autowired
    private TravelPlanService travelPlanService;

    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "旅游攻略-列表")
    @GetMapping("/list")
    @ApiAuth(ignore = true)
    public Result<Page<TravelPlanWebListVO>> list(TravelPlanUserListQuery query) {
        LambdaQueryWrapper<TravelPlan> queryWrapper = travelPlanService.toUserListWrapper(query);
        Page<TravelPlan> pageData = travelPlanService.page(query.toPage(), queryWrapper);
        List<TravelPlan> travelPlanList = pageData.getRecords();
        if (CollUtil.isEmpty(travelPlanList)) {
            return Result.data(new Page<>());
        }
        int userId = WebContext.getUserId();
        List<TravelPlanWebListVO> results = travelPlanService.toWebListVo(travelPlanList, userId);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "旅游攻略-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "旅游攻略id"),
    })
    @GetMapping("/detail")
    @ApiAuth(ignore = true)
    public Result<TravelPlanWebVO> detail(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlanWebVO planWebVO = travelPlanService.getByIdToWeb(id);
        int userId = WebContext.getUserId();
        commentService.setInteractionStatus(planWebVO, userId, planWebVO.getId(), LinkType.TRAVEL_PLAN);
        return Result.data(planWebVO);
    }

    @ApiOperation(value = "旅游攻略-增加")
    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated TravelPlanAddDTO travelPlanAddDTO) {
        travelPlanService.add(travelPlanAddDTO, WebContext.getUserId());
        return Result.ok("添加旅游攻略成功");
    }

    @ApiOperation(value = "旅游攻略-更新")
    @PostMapping("/update")
    public Result<String> update(@RequestBody @Validated TravelPlanUpdateDTO travelPlanUpdateDTO) {
        travelPlanService.update(travelPlanUpdateDTO, WebContext.getUserId());
        return Result.ok("更新旅游攻略成功");
    }

    @ApiOperation(value = "旅游攻略-编辑详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "旅游攻略id"),
    })
    @GetMapping("/editDetail")
    public Result<TravelPlanWebVO> editDetail(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        int userId = WebContext.getUserId();
        TravelPlanWebVO planWebVO = travelPlanService.getByIdToCreator(id, userId);
        commentService.setInteractionStatus(planWebVO, userId, planWebVO.getId(), LinkType.TRAVEL_PLAN);
        return Result.data(planWebVO);
    }


    @ApiOperation(value = "旅游攻略-用户作品列表")
    @GetMapping("/workList")
    public Result<Page<TravelPlanWebListVO>> workList(TravelPlanUserListQuery query) {
        int userId = WebContext.getUserId();
        LambdaQueryWrapper<TravelPlan> queryWrapper = travelPlanService.toUserWorksListWrapper(query, userId);
        Page<TravelPlan> pageData = travelPlanService.page(query.toPage(), queryWrapper);
        List<TravelPlan> travelPlanList = pageData.getRecords();
        if (CollUtil.isEmpty(travelPlanList)) {
            return Result.data(new Page<>());
        }
        List<TravelPlanWebListVO> results = travelPlanService.toWebListVo(travelPlanList, userId);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "旅游攻略-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "旅游攻略id"),
    })
    @DeleteMapping("/delete")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(TravelPlanService.TRAVEL_PLAN_NOT_EXIST);
        }
        TravelPlan travelPlan = travelPlanService.getById(id);
        if (travelPlan == null) {
            return Result.error("攻略已不存在!");
        }
        int userId = WebContext.getUserId();
        if (travelPlan.getCreatorId() != userId) {
            return Result.error("不能删除其他人的作品!");
        }
        travelPlanService.removeById(id);
        return Result.ok("删除成功");
    }


}
